<?php
/**
* @version		$Id: default_viewfrontend.php 118 2012-10-02 08:52:27Z michel $
* @package		Cashbag
* @subpackage 	Views
* @copyright	Copyright (C) 2013, . All rights reserved.
* @license #
*/

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

 
class CashbagViewCashbag  extends JViewLegacy 
{
	public function display($tpl = null)
	{
		
		$app = &JFactory::getApplication('site');
		$document	= &JFactory::getDocument();
		$uri 		= &JFactory::getURI();
		$user 		= &JFactory::getUser();
		$pagination	= &$this->get('pagination');
		$params		= $app->getParams();				
		$menus          = &JSite::getMenu();
                $task           = $app->input->get('task');
		$layoutName = JRequest::getWord('layout', 'default');
                
                if($layoutName == 'proposition'){
                    
                    $this->_display_prop();
                    return;
                }
                
                $this->setLayout($layoutName);
                
		$menu	= $menus->getActive();
               
                $model  = new CashbagModelCashbag();
                
                switch($task){
                    
                    case'request':
                    {
                        $model->confirmer_cgu();
                        $array = $model->demanderSac();
                        
                        if(count($array)>0){//On a faire une etiquette
                           
                            $link   = 'https://elabel.kiala.com/elabel/generatePDF?sender.name='.$array->last_name
                                        . '&sender.firstname='.$array->first_name.'&sender.street='.$array->address_1.' '.$array->address_2.'&sender.city='.$array->city.'&sender.zip='.$array->zip
                                        . '&barcode='.$array->ref_interne.'&customerNumber=&parcelNumber=&dspid=33601028&nb=1&type=elabel&barcodeType=Code128';
                            
                            $lien   =  '<a class="btn_cash_green" target ="_blank"'
                                        . 'href="'.$link.'">'
                                    . 'Imprimez votre étiquette</a>';
                           
                        }
                        else{
                            $lien   = '<h4 class="alert alert-success center_h2">Vous recevrez votre sac CASHBAG sous 4 jours</h4>';
                        }
                        
                        $this->assignRef('lien', $lien);
                        //kiala
                         $carte   = $model->carte_kiala();
                        
                        $url = "http://locateandselect.kiala.com/search?dspid=DEMO_DSP&country=FR&language=fr&preparationdelay=&street=$carte[2]&zip=$carte[0]&city=$carte[1]&bckUrl=testbckurl.html%3Forder%3D12345%26&target=_parent&select-text=&gui=&css=&header=&thumbnails=&map=&map-controls=&align=&pl=map&sort-method=&max-result=&zipfilter";
                        
                        $points = '<a href="'.$url.'" target="_blank"><h3>Identifiez le point KIALA le plus proche de chez vous</h3>';
                        $points .= '<img src="'.JPATH_BASE.DS.'images'.DS.'kiala_logo.jpg" /></a>';
                        //var_dump($points);
                        $this->assignRef('point_kiala',$points);
                        break;
                    }
                   
                    case'info':
                    {
                        $items=  $model->displayInfo();
                        $this->assignRef( 'items', $items );
                        break;
                    }
                    case'accord':
                    {
                        $result = $model->modifier_publish();
                        if(!$result){
                            $app =& JFactory::getApplication();
                            $app->redirect('index.php');
                        }
                        break;
                    }
                    case'validate_addr':
                    {
                        $data   = $model->getAddrBag();
                        $this->assignRef('address',$data);
                        break;
                    }
                    case'saveaddr':
                    {
                       $model->saveNewAddr();
                        break;
                    }
                    default:
                        
                        if (is_object( $menu )) {
                             $menu_params = $menus->getParams($menu->id) ;
                             if (!$menu_params->get( 'page_title')) {
                                     $params->set('page_title', 'Cashbag');
                             }
                     }		


                     //$item = $this->get( 'Item' );
                    // $this->assignRef( 'item', $item );

                     //$this->assignRef('params', $params);
                     //$this->assignRef('pagination', $pagination);
                    
		}
                
		parent::display($tpl);
	}
        
        function _display_prop(){
            
            $model  = new CashbagModelCashbag();
            
            $check  = $model->check_locked();
            //var_dump($check);
            if(!isset($check->id)){//si pas de reference trouvée en BD, erreur!
                $this->setLayout('noreference');
                
            }
            else{
               $now =  new DateTime("now");
               $locked  = new DateTime($check->date_locked);
               
               if($locked->diff($now)>3){//au dela de 3 jours, la proposition a expiré!
                    $this->setLayout('expired');
               }
               else{
                   $this->setLayout('proposition');
                   $this->assignRef('id', $check->id);
               }
            }
            parent::display($tpl);
        }
       /* function demander_sac(){
            
        }*/
}
?>